﻿using System.IO;
using System;
using log4net.Appender;
using log4net.Config;
using log4net;
using System.Diagnostics;
namespace BaseLibrary
{
    public class LogHelper
    {

        /// <summary>
        /// 
        /// </summary>
        /// 
        public  string PlugName { get; set; }
        static LogHelper()
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            else
            {
                RollingFileAppender appender = new RollingFileAppender();
                appender.Name = "root";
                appender.File = "log.txt";
                appender.AppendToFile = true;
                appender.RollingStyle = RollingFileAppender.RollingMode.Composite;
                appender.DatePattern = "yyyyMMdd-HHmm\".txt\"";
                appender.MaximumFileSize = "1MB";
                appender.MaxSizeRollBackups = 10;
                log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%d{yyyy-MM-dd HH:mm:ss,fff}[%t] %-5p [%c] : %m%n");
                appender.Layout = layout;
                BasicConfigurator.Configure(appender);
                appender.ActivateOptions();
            }
        }
        public  void Debug(object message)
        {

            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            ILog logger = LogManager.GetLogger(new StackFrame(1, true).GetMethod().DeclaringType.FullName);
            if (logger.IsDebugEnabled)
            {
                logger.Debug(PlugName + message);
            }
            logger = null;
            LogManager.GetRepository().Shutdown();

        }
        public   void Error(object message)
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            ILog logger = LogManager.GetLogger(new StackFrame(1, true).GetMethod().DeclaringType.FullName);
            if (logger.IsErrorEnabled)
            {
                logger.Error(PlugName + message);
            }
            logger = null;

            LogManager.GetRepository().Shutdown();
        }
        public   void Fatal(object message)
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            ILog logger = LogManager.GetLogger(new StackFrame(1, true).GetMethod().DeclaringType.FullName);
            if (logger.IsFatalEnabled)
            {
                logger.Fatal(PlugName + message);
            }
            logger = null;
            LogManager.GetRepository().Shutdown();

        }

        public   void Info(object message)
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            ILog logger = LogManager.GetLogger(new StackFrame(1, true).GetMethod().DeclaringType.FullName);
            if (logger.IsInfoEnabled)
            {
                logger.Info(PlugName + message);
            }
            logger = null;

            LogManager.GetRepository().Shutdown();
        }

        public   void Warn(object message)
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
            ILog logger = LogManager.GetLogger(new StackFrame(1, true).GetMethod().DeclaringType.FullName);

            if (logger.IsWarnEnabled)
            {
                logger.Warn(PlugName + message);
            }
            logger = null;
            LogManager.GetRepository().Shutdown();

        }
    }
}